package com.zkyc.framework.file.util;

import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.zkyc.framework.common.utils.CmdExecuter;
import org.bytedeco.javacpp.Loader;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import static com.zkyc.framework.common.utils.FFMPEG.returnSecond;

/**
 * @Author libingchuan  @Description: 提取视频第一帧
 */
public class FFMPEGUtil {

    /**
     * 提取主方法
     *
     * @param path   MP4视频路径
     * @param tmpDir 临时目录
     * @return 视频第一帧
     * @throws Exception 异常
     */
    public static String extract(String path, String tmpDir) throws Exception {
        String mp4Path;
        if (path.startsWith("http")) {
            mp4Path = tmpDir + "/" + IdUtil.simpleUUID() + ".mp4";
            HttpUtil.downloadFile(path, mp4Path);
        } else {
            mp4Path = path;
        }
        File file = new File(tmpDir);
        if (file.exists()) {
            file.delete();
        }

        return ffmpegExtractImage(mp4Path, tmpDir)
                .orElseThrow(() -> new Exception("提取失败"));
    }

    /**
     * 提取视频第一帧图片
     *
     * @param mp4Path 视频地址
     * @param picPath 图片地址
     * @return 提取的图片地址
     */
    public static Optional<String> ffmpegExtractImage(String mp4Path, String picPath) {
        String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);

        ProcessBuilder extractBuilder =
                new ProcessBuilder(
                        ffmpeg, "-i", mp4Path, "-f", "image2", "-ss", "1", "-frames:v", "1", picPath);
        try {
            extractBuilder.inheritIO().start().waitFor();
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
            return Optional.empty();
        }
        // 返回图片文件路径
        return Optional.of(picPath);
    }

    /**
     * 打图片水印
     *
     * @return 提取的图片地址
     */
    public static String watermark(HashMap<String, String> dto) {
        String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
        List<String> cmd = new ArrayList<String>();
        cmd.add(ffmpeg);
        cmd.add("-y");
        cmd.add("-i");
        cmd.add(dto.get("input_path"));
        if (null != dto.get("screen_size")) {
            cmd.add("-s");
            cmd.add(dto.get("screen_size"));
        }
        if (null != dto.get("logo")) {
            String logo = dto.get("logo");
            cmd.add("-vf");
            String xaxis = dto.get("xaxis");
            String yaxis = dto.get("yaxis");
            xaxis = xaxis != null && !xaxis.equals("") ? xaxis : "0";
            yaxis = yaxis != null && !yaxis.equals("") ? yaxis : "0";
            //水印在右下角
            //xaxis = "main_w-overlay_w-10:main_h-overlay_h-10";
            //水印在中间
            xaxis = "main_w/2-overlay_w/2:main_h/2-overlay_h/2";
            String logoString;
            if (!StringPool.ZERO.equals(dto.get("height"))) {
                Integer height = Integer.parseInt(dto.get("height")) / 10;
                logoString = "movie=" + logo + ",scale=-1:" + height + "[logo],[in][logo]overlay=x=" + xaxis + "[out]";
            } else {
                logoString = "movie=" + logo + "[logo],[in][logo]overlay=x=" + xaxis + "[out]";
            }
            //String logoString = "movie=" + logo + "[logo],[in][logo]overlay=x=" + xaxis + ":y=" + yaxis + "[out]";
            cmd.add(logoString);
        }
        cmd.add("-strict");
        cmd.add("-2");
        if (null != dto.get("vb") && !dto.get("vb").equals("")) {
            cmd.add("-vb");
            cmd.add(dto.get("vb") + "k");
        }
        if (null != dto.get("ab") && !dto.get("ab").equals("")) {
            cmd.add("-ab");
            cmd.add(dto.get("ab") + "k");
        }
        cmd.add("-q:v");
        cmd.add("4");
        cmd.add(dto.get("video_converted_path"));
        String converted_time = CmdExecuter.exec(cmd);
        return returnSecond(converted_time);// 获取转换时间

    }


}